import {Component, OnDestroy, OnInit} from '@angular/core';
import {SERVER_URL} from '../../config';
import {HttpClient} from '@angular/common/http';
import {NbToastStatus} from '@nebular/theme/components/toastr/model';
import {NbToastrService} from '@nebular/theme';
import {NbDialogService} from '@nebular/theme';
import {ProduceAddComponent} from '../produce-add/produce-add.component';

export interface ReturnData {
  ret: number;
  data: any;
  msg: string;
}

@Component({
  selector: 'app-produce-find',
  templateUrl: './produce-find.component.html',
  styleUrls: ['./produce-find.component.css'],

})

export class ProduceFindComponent implements OnInit {
  date = new Date();
  totalProduceList: any;
  chosenDate: any;

  constructor(private http: HttpClient,
              private toastrService: NbToastrService,
              private dialogService: NbDialogService,
  ) {
  }

  ngOnInit() {
    this.loadData();
  }

  async loadData() {
    const data = await this.http.get<ReturnData>(SERVER_URL + '?s=App.Manualready.Select').toPromise();
    this.totalProduceList = data.data.code;
    console.log(this.totalProduceList);
  }

  onDateChanged(event) {
    console.log(event);
    event.setMinutes(event.getMinutes() - event.getTimezoneOffset());
    this.chosenDate = event.toISOString().substring(0, 10);
  }

  reset() {
    this.chosenDate = null;
  }

  addProduce() {
    const dialogRef = this.dialogService.open(ProduceAddComponent).onClose.subscribe(data => {
      if (data) {
        this.loadData();
      }
    });

  }

}
